# Maintainer: Natanael Copa <ncopa@alpinelinux.org>

_flavor=lts
pkgname=linux-${_flavor}
pkgver=5.4.54
case $pkgver in
	*.*.*)	_kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=2
pkgdesc="Linux lts kernel"
url="https://www.kernel.org"
depends="mkinitfs"
_depends_dev="perl gmp-dev elfutils-dev bash flex bison"
makedepends="$_depends_dev sed installkernel bc linux-headers linux-firmware-any openssl-dev
	diffutils"
options="!strip"
_config=${config:-config-lts.${CARCH}}
install=
source="https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/linux-$_kernver.tar.xz
	0002-powerpc-config-defang-gcc-check-for-stack-protector-.patch

	0001-arm64-dts-lx2160a-add-mdio-device-tree-nodes.patch
	0001-arm64-dts-lx2160a-add-lx2160acex7-device-tree-build.patch
	0002-arm64-dts-lx2160a-add-lx2160acex7-device-tree.patch
	0005-arm64-dts-lx2160a-cex7-add-ltc3882-support.patch
	0006-arm64-dts-lx2160a-cex7-add-on-module-eeproms.patch
	0007-pci-hotplug-declare-IDT-bridge-as-hotpluggabl-bridge.patch
	0008-pci-spr2803-quirk-to-fix-class-ID.patch

	config-lts.aarch64
	config-lts.armv7
	config-lts.x86
	config-lts.x86_64
	config-lts.ppc64le
	config-lts.s390x
	config-lts.mips64
	config-lts.mips

	config-virt.aarch64
	config-virt.armv7
	config-virt.ppc64le
	config-virt.x86
	config-virt.x86_64
	"
subpackages="$pkgname-dev:_dev:$CBUILD_ARCH"
_flavors=
for _i in $source; do
	case $_i in
	config-*.$CARCH)
		_f=${_i%.$CARCH}
		_f=${_f#config-}
		_flavors="$_flavors ${_f}"
		if [ "linux-$_f" != "$pkgname" ]; then
			subpackages="$subpackages linux-${_f}::$CBUILD_ARCH linux-${_f}-dev:_dev:$CBUILD_ARCH"
		fi
		;;
	esac
done

if [ "${pkgver%.0}" = "$pkgver" ]; then
	source="$source
	https://cdn.kernel.org/pub/linux/kernel/v${pkgver%%.*}.x/patch-$pkgver.xz"
fi
arch="all !armhf"
license="GPL-2.0"

_carch=${CARCH}
case "$_carch" in
aarch64*) _carch="arm64" ;;
arm*) _carch="arm" ;;
mips*) _carch="mips" ;;
ppc*) _carch="powerpc" ;;
s390*) _carch="s390" ;;
esac

prepare() {
	local _patch_failed=
	cd "$srcdir"/linux-$_kernver
	if [ "$_kernver" != "$pkgver" ]; then
		msg "Applying patch-$pkgver.xz"
		unxz -c < "$srcdir"/patch-$pkgver.xz | patch -p1 -N
	fi

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# remove localversion from patch if any
	rm -f localversion*
	oldconfig
}

oldconfig() {
	for i in $_flavors; do
		local _config=config-$i.${CARCH}
		local _builddir="$srcdir"/build-$i.$CARCH
		mkdir -p "$_builddir"
		echo "-$pkgrel-$i" > "$_builddir"/localversion-alpine \
			|| return 1

		cp "$srcdir"/$_config "$_builddir"/.config
		make -C "$srcdir"/linux-$_kernver \
			O="$_builddir" \
			ARCH="$_carch" \
			listnewconfig oldconfig
	done
}

build() {
	unset LDFLAGS
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"
	for i in $_flavors; do
		cd "$srcdir"/build-$i.$CARCH
		make ARCH="$_carch" CC="${CC:-gcc}" \
			KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
	done
}

_package() {
	local _buildflavor="$1" _outdir="$2"
	local _abi_release=${pkgver}-${pkgrel}-${_buildflavor}
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

	cd "$srcdir"/build-$_buildflavor.$CARCH
	# modules_install seems to regenerate a defect Modules.symvers on s390x. Work
	# around it by backing it up and restore it after modules_install
	cp Module.symvers Module.symvers.backup

	mkdir -p "$_outdir"/boot "$_outdir"/lib/modules

	local _install
	case "$CARCH" in
		arm*|aarch64) _install="zinstall dtbs_install";;
		*) _install=install;;
	esac

	make -j1 modules_install $_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$_outdir" \
		INSTALL_PATH="$_outdir"/boot \
		INSTALL_DTBS_PATH="$_outdir/boot/dtbs-$_flavor"

	cp Module.symvers.backup Module.symvers

	rm -f "$_outdir"/lib/modules/${_abi_release}/build \
		"$_outdir"/lib/modules/${_abi_release}/source
	rm -rf "$_outdir"/lib/firmware

	install -D include/config/kernel.release \
		"$_outdir"/usr/share/kernel/$_buildflavor/kernel.release
}

# main flavor installs in $pkgdir
package() {
	depends="$depends linux-firmware-any"

	_package lts "$pkgdir"
}

# subflavors install in $subpkgdir
virt() {
	_package virt "$subpkgdir"
}

_dev() {
	local _flavor=$(echo $subpkgname | sed -E 's/(^linux-|-dev$)//g')
	local _abi_release=${pkgver}-${pkgrel}-$_flavor
	# copy the only the parts that we really need for build 3rd party
	# kernel modules and install those as /usr/src/linux-headers,
	# simlar to what ubuntu does
	#
	# this way you dont need to install the 300-400 kernel sources to
	# build a tiny kernel module
	#
	pkgdesc="Headers and script for third party modules for $_flavor kernel"
	depends="$_depends_dev"
	local dir="$subpkgdir"/usr/src/linux-headers-${_abi_release}
	export KBUILD_BUILD_TIMESTAMP="$(date -Ru${SOURCE_DATE_EPOCH:+d @$SOURCE_DATE_EPOCH})"

	# first we import config, run prepare to set up for building
	# external modules, and create the scripts
	mkdir -p "$dir"
	cp "$srcdir"/config-$_flavor.${CARCH} "$dir"/.config
	echo "-$pkgrel-$_flavor" > "$dir"/localversion-alpine

	make -j1 -C "$srcdir"/linux-$_kernver O="$dir" ARCH="$_carch" \
		syncconfig prepare modules_prepare scripts

	# remove the stuff that points to real sources. we want 3rd party
	# modules to believe this is the soruces
	rm "$dir"/Makefile "$dir"/source

	# copy the needed stuff from real sources
	#
	# this is taken from ubuntu kernel build script
	# http://kernel.ubuntu.com/git/ubuntu/ubuntu-zesty.git/tree/debian/rules.d/3-binary-indep.mk
	cd "$srcdir"/linux-$_kernver
	find .  -path './include/*' -prune \
		-o -path './scripts/*' -prune -o -type f \
		\( -name 'Makefile*' -o -name 'Kconfig*' -o -name 'Kbuild*' -o \
		   -name '*.sh' -o -name '*.pl' -o -name '*.lds' -o -name 'Platform' \) \
		-print | cpio -pdm "$dir"

	cp -a scripts include "$dir"

	find $(find arch -name include -type d -print) -type f \
		| cpio -pdm "$dir"

	install -Dm644 "$srcdir"/build-$_flavor.$CARCH/Module.symvers \
		"$dir"/Module.symvers

	mkdir -p "$subpkgdir"/lib/modules/${_abi_release}
	ln -sf /usr/src/linux-headers-${_abi_release} \
		"$subpkgdir"/lib/modules/${_abi_release}/build
}

sha512sums="9f60f77e8ab972b9438ac648bed17551c8491d6585a5e85f694b2eaa4c623fbc61eb18419b2656b6795eac5deec0edaa04547fc6723fbda52256bd7f3486898f  linux-5.4.tar.xz
d19365fe94431008768c96a2c88955652f70b6df6677457ee55ee95246a64fdd2c6fed9b3bef37c29075178294a7fc91f148ead636382530ebfa822be4ad8c2f  0002-powerpc-config-defang-gcc-check-for-stack-protector-.patch
cfcd03668a80944706c66f89055f641c972c1839bf33123c19d63681d3d3958c8a0d4ab0679af9b921d0e9fd0c25445b8c417a21d1519e3d196780a0d5bd1520  0001-arm64-dts-lx2160a-add-mdio-device-tree-nodes.patch
1c87556c43b20c70e40d628624f0043a7dea1c3e66be26e15f594a1aed449a11f214795617d25309e1c1d5af92ec644222c4e68be652be043bdda80af0c0ece2  0001-arm64-dts-lx2160a-add-lx2160acex7-device-tree-build.patch
e75ab07e3d2d0d3fd085fb4132b3fee1650ae3bf1b6eaefe1f92b90071547a4f3ceef35ad4ce0f9345e78fac2de1697f6db3de1a526e730262be7ff299bd0038  0002-arm64-dts-lx2160a-add-lx2160acex7-device-tree.patch
d27bf4b8169ac047f60beae326da8f5e4d4f887c893cb4dd97f87d0c363d02e00704bf07d2026d9fd622b612cd9abbebfe4c5adfb7c033d73939b959e9652add  0005-arm64-dts-lx2160a-cex7-add-ltc3882-support.patch
926cb8f9a15f82bb90db962c9b7f7cbb3dbb754d3b1a0d7d12c0593ea60de8f07744ccd7c86e0e43daf1eab3392cc8624786680e1244fa96982e676a908ab88d  0006-arm64-dts-lx2160a-cex7-add-on-module-eeproms.patch
ca5aafac37e0b5f3fcbaf801e12f98beb58ffaf1d8c88f76caff22b059831869b4094e7fdcb6d6860422d6b2d036e072caff460e1feb84bd04d10740ad56265b  0007-pci-hotplug-declare-IDT-bridge-as-hotpluggabl-bridge.patch
cbe85cf34e8420c91d2276c2d2aa0ab5023af68e57a1fa613f073f16a76766c67f585eda71c28f232bd0625e0dc8275a9eddc95f49409205dc0dbcc28c9fac1c  0008-pci-spr2803-quirk-to-fix-class-ID.patch
7d4cfd66da1f5540da1b785ba9f3b79157bccc0efd2e72c7b9437895ad785750df4ae7ad8dbf185bcd514d1d96758a65e7443c1239f40d67434e8b68212cc5f3  config-lts.aarch64
e2061c4f65c899f2a39a21feb80555a1861f157ee1238261965ef7cd2e97db7f8c0f78cc0de8ca36d8b851171364831992d1535555acd9d26c24aa06f4e06db3  config-lts.armv7
8ed190ce238e1017a1ff53cf701bdb2009c467e09c86b80450e47003dcb8ba4b7bc48d5e23ee489de2c574b977cfffe73e314ced38906097b7377a4955eacfef  config-lts.x86
d7933d152c92e2aa4282eb4599eaed171ede7edc7776d0bb6cf85d796ff52366ce19c403f7d72da29d3b4ee6e55cb4936d41d8751b08f6d4f20b57025f587fe3  config-lts.x86_64
ede2ae8423e2d46e703f38264d3ddbe0f103e94991e63a35879d8951826e27d29f1cdab48a0f168cc401efad48b7db793be7e72bd06f91edf65bff5dab604041  config-lts.ppc64le
a30f4473fcc9fd29d0abcef482c0aa0999cbfe1325f00f42363f733cdbfe342abdda12e3c208908821a7b7343b48da0493117005c26d141afaadf0dc705c025b  config-lts.s390x
660c4a0d0a12769114c5e9efd0e688fdac4bfe37d5a6ee3c05cc809051518b72b23fbdbf4d18afdff44efcd439d325ebe1b923a4d1cb537bdc486e6677d210c6  config-lts.mips64
78ce71eb1842c085aa2b2fbac06ebcfc83864a38143bdadd1019de464c487a7a5c48cbfa96f449dc8e7e2574cea4c51cc42aea6dba80f0ee56b7b2e4424f5941  config-lts.mips
72617fae992c52fb1d2a10e91caa6f5172eab3555392c6fcdcadad78b7c6083e619b8993353f09ddfae2afd025b2927d76f571f184329e6ee38b7049c6165ad3  config-virt.aarch64
6c2d5c6ba6bd3a4a726dc6bc3f519a7f445f2c7d1631fe726377ebafa03a1c5d9c015f9999815b5389820e6615abc13cbf9d1513dd722e683eb9104480aeb71d  config-virt.armv7
91297ccf87ec77c45300b9fba539f8985a7dd60c39f698b6ee837f9a23edbe64d8b23beebd3b2bce085b5989d68645513367634b92f630800686f6c74fbf68b7  config-virt.ppc64le
358a3114829994b2a5d56005e092557ae7f27ec672196d41e0b56c3f68698bc1eee3e01ac82cce86fcd691c3305cb043558e9a2eb603274fb853e04b02795413  config-virt.x86
577d20c246c1add68b532f77a8444ef04933d95cf694487f49bd2430bf3e41255bc69a264ff561b39c10ca35f6e1a1b03efe92a6487e1b3e8db516611301d413  config-virt.x86_64
bd365f01e5aace4fe6322d7ab7d93df93779c1e1520e650d39fd49802dc3554b89c889830d112e19b6ce531d00bcef77f55f4a4d36e5e84224a11a1205e43789  patch-5.4.54.xz"
